﻿<?php include("conn.php");
mysql_query('SET NAMES UTF8'); 

	  

class MyPrizeBean
{
	public $num;
	public $ordersid;
	public $zongjia;
	public $allshopprice;

	function __construct($num,$ordersid,$zongjia,$allshopprice)
	{
		$this->num      = $num;
		$this->ordersid      = $ordersid;
		$this->zongjia      = $zongjia;
		$this->allshopprice      = $allshopprice;
	}
}
$prize = array();

 if($_REQUEST[userid]!="")
 {         
 
    $stat=0;
	$state=1;
	
if ($_REQUEST[addressid]!='') {
	$rert322=mysql_query("select *from address where  id='".$_REQUEST[addressid]."' and  userid ='".$_REQUEST[userid]."'");
}else{
	$rert322=mysql_query("select *from address where  state ='".$state."' and  userid ='".$_REQUEST[userid]."'");
}
$di=mysql_fetch_array($rert322);

	$address=$di[province].",".$di[city].",".$di[address].",".$di[name].",".$di[tel];
	
$state=0;

    function build_order_no(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
$numss=build_order_no();

$date=date("Y-m-d H:i:s");
// 获取carid 
$carid = $_REQUEST[carid];
// 获取carid end
	$rert30=mysql_query("select DISTINCT shopid from car where userid='".$_REQUEST[userid]."' and state='".$stat."'");
$allshopprice=0;

while($rowf=mysql_fetch_array($rert30)){

	$zongj=0;

	$query=mysql_query("select *from car where shopid='".$rowf[shopid]."' and userid='".$_REQUEST[userid]."' and carid in (".$carid.") and state='".$stat."'");

	$rert31=mysql_query("select *from shop where  shopid ='".$rowf[shopid]."'");
$rowf1=mysql_fetch_array($rert31);
while($car=mysql_fetch_array($query)){
	$zongj=$zongj+$car[price]*$car[number]+$rowf1[distribution_fee];
}
$allshopprice+=$zongj;

// 是否购买运费险并计算商品总价 
if ($_REQUEST[buyFreightRisk]==1) {
	// 查找运费险
	$query = mysql_query("select freightRisk from xitong where id=1");
	$freightRisk = mysql_fetch_assoc($query);
	// 查找运费险 end
	$zongj=$zongj+$freightRisk[freightRisk];
	$buyFreightRisk = 1;

}else{
	$buyFreightRisk = 0;
}
// 是否购买运费险并计算商品总价 end

$srat=mysql_query("INSERT INTO goodsorders(numbers,totalprice,userid,address,addtime,shopid,buyFreightRisk) VALUES ('$numss','$zongj','$_REQUEST[userid]','$address','$date','$rowf[shopid]','$buyFreightRisk')");

$dingid=mysql_insert_id();

// 调用信息推送接口
$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$dingid;
file_get_contents($url);
// 调用信息推送接口 end

$ordersid.= ','.$dingid;
$state=7;
$sats=0;
$srat1=mysql_query("update car set ordersid='".$dingid."' where state='".$sats."' and shopid='".$rowf[shopid]."' and userid='".$_REQUEST[userid]."'");
$srat1=mysql_query("update car set state='".$state."' where state='".$sats."' and shopid='".$rowf[shopid]."' and userid='".$_REQUEST[userid]."'");
	
}
$ordersid = trim($ordersid,',');
	      if($srat==true)
	     {
		  $der=100;
		  
		  }
	
		}
		 else{
		   $der=300;
		  }
$prize[] = new MyPrizeBean($numss,$ordersid,$zongj,$allshopprice);
echo json_encode($prize);


?>